<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager,
                 com.scalar.chiptrack.users.Functionality,
                 com.scalar.chiptrack.users.User,
                 com.scalar.forecast.commons.Constants,
                 com.scalar.forecast.commons.SessionKeys"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<% User user1 = (User) session.getAttribute (SessionKeys.USER_KEY); %>
<HTML>
<HEAD>
<TITLE> User Defined Fields </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<%String chipTracContext = "/" + ConfigManager.getChipTracContext();%>
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel=stylesheet>
<SCRIPT src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></SCRIPT>
<SCRIPT>
var fieldName = "";
var fieldLabelName = "";
function openFieldValuesWindow( ctrl )
{
    errors = "";
    var pa  = document.getElementById ( 'errorsId' );
	fieldName = trim ( ctrl.value );
    if ( fieldName == '' )
    {
        errors = errors + "<li> Please Enter List1 Name</li>";
        pa.innerHTML = errors;
        ctrl.focus();
        ctrl.select();
        return;
    }
    fieldLabelName = ctrl.name;
	newWindow = window.open ('<%=chipTracContext%>/jsp/forecast/forecast_user_defined_field_values_entry.jsp','exportToFilePopup','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=yes,scrollbars=yes,width=300 height=350,resizable=yes,left=375,top=180');
	newWindow.focus();
}
function closeWindow()
{
    window.opener.enableFields();
    window.opener.onSubmit( 'reloadForecastEntryFields' );
	window.close();
}
function updateFieldValues( ctrlName, idx, value )  //this method will be called by child window.
{
    if ( idx == 0 )
    {

        if ( ctrlName == 'userList1Values' )
        {
            userList1Values = new Array();
            userList1Values[idx] = value;
        }
    }
    else
    {
        if ( ctrlName == 'userList1Values' )
        {
            userList1Values[idx] = value;
        }
    }
}
function isValidUserFieldNames()
{
    var form = document.forecastSetupUserDefinedFieldsForm;
    form.userList1.value = trim ( form.userList1.value );
    form.userDate1.value = trim ( form.userDate1.value );
    form.userText1.value = trim ( form.userText1.value );
    form.userText2.value = trim ( form.userText2.value );

    var userList1Val = form.userList1.value;
    var userDate1Val = form.userDate1.value;
    var userText1Val = form.userText1.value;
    var userText2Val = form.userText2.value;

    isValid = true;
    if ( userList1Val.toUpperCase() == 'QTY' || userList1Val.toUpperCase() == 'PRICE' || userList1Val.toUpperCase() == 'CONFIDENCE LEVEL' )
    {
        errors = errors + "<li>List1 can not be Qty / Price / Confidence Level</li>"
        isValid = false;
    }
    if ( userDate1Val.toUpperCase() == 'QTY' || userDate1Val.toUpperCase() == 'PRICE' || userDate1Val.toUpperCase() == 'CONFIDENCE LEVEL' )
    {
        errors = errors + "<li>Date Field Name can not be Qty / Price / Confidence Level</li>"
        isValid = false;
    }
    if ( userText1Val.toUpperCase() == 'QTY' || userText1Val.toUpperCase() == 'PRICE' || userText1Val.toUpperCase() == 'CONFIDENCE LEVEL' )
    {
        errors = errors + "<li>Text1 Field Name can not be Qty / Price / Confidence Level</li>"
        isValid = false;
    }
    if ( userText2Val.toUpperCase() == 'QTY' || userText2Val.toUpperCase() == 'PRICE' || userText2Val.toUpperCase() == 'CONFIDENCE LEVEL' )
    {
        errors = errors + "<li>Text2 Field Name can not be Qty / Price / Confidence Level</li>"
        isValid = false;
    }

    if ( trim ( form.userList1.defaultValue ) != '' && userList1Val == '' )
    {
        errors = errors + "<li>Please Enter List1 Name</li>"
        isValid = false;
    }
    if ( trim ( form.userDate1.defaultValue ) != '' && userDate1Val == '' )
    {
        errors = errors + "<li>Please Enter Date Field1 Name</li>"
        isValid = false;
    }
    if ( trim ( form.userText1.defaultValue ) != '' && userText1Val == '' )
    {
        errors = errors + "<li>Please Enter Text Field1 Name</li>"
        isValid = false;
    }
    if ( trim ( form.userText2.defaultValue ) != '' && userText2Val == '' )
    {
        errors = errors + "<li>Please Enter Text Field2 Name</li>"
        isValid = false;
    }

    //checking duplicates

    if (  userList1Val != '' && ( userList1Val == userDate1Val ||  userList1Val == userText1Val || userList1Val == userText2Val ) )
    {
        errors = errors + "<li>User Defined Fields should not be repeated</li>"
        isValid = false;
    }
    else if (  userDate1Val != '' && ( userDate1Val == userText1Val || userDate1Val == userText2Val ) )
    {
        errors = errors + "<li>User Defined Fields should not be repeated</li>"
        isValid = false;
    }
    else if (  userText1Val != '' && ( userText1Val == userText2Val ) )
    {
        errors = errors + "<li>User Defined Fields should not be repeated</li>"
        isValid = false;
    }

    return isValid;
}
var errors = "";
function onSubmit()
{
    errors = "";
    var pa  = document.getElementById ( 'errorsId' );
    var pa1 = document.getElementById ( 'errorMessagesId' );
    var pa2 = document.getElementById ( 'infoMessagesId' );
    pa.innerHTML = "";
    if ( !isValidUserFieldNames() )
    {
        pa1.innerHTML = "";
        pa2.innerHTML = "";
        pa.innerHTML = errors;
        return;
    }
    createHiddenFields( userList1Values, 'userList1Values' );
    var form = document.forecastSetupUserDefinedFieldsForm;
    form.action = "forecastUserDefinedFields.do?action=save";
    form.submit();
}
function createHiddenFields ( arr, hiddenFieldName )
{
    form = document.forecastSetupUserDefinedFieldsForm;
    elmt = document.getElementById( 'hiddenFieldsId' );
    for ( i = 0; i< arr.length; i++)
    {
        elmt.innerHTML = elmt.innerHTML + "<input type='hidden' name='" + hiddenFieldName +"' value='" + arr[i] + "'>";
    }
}
var userList1Values = new Array();
</SCRIPT>

</HEAD>

<BODY>
<center>
<html:form action="/forecastUserDefinedFields" name="forecastSetupUserDefinedFieldsForm" type="com.scalar.forecast.forecastSetup.forms.UserDefinedFieldsForm">
    <script>
        <logic:present  name="forecastSetupUserDefinedFieldsForm" property="userList1Values">
            <logic:iterate name="forecastSetupUserDefinedFieldsForm" property="userList1Values" id="userList1Value" indexId="idx" >
                <logic:present name="userList1Value">
                    userList1Values[<%=idx%>] ='<bean:write name="userList1Value"/>';
                </logic:present>
            </logic:iterate>
        </logic:present>
    </script>
<TABLE border="0">
    <% if ( !user1.isActionAllowed ( Functionality.FORECAST_SETUP,User.USER_ACTION_MODIFY ) ) { %>
    <tr><td colspan=6 align='left'><font class='NArialS'><font color='blue'><b><%=Constants.NO_MODIFY_RIGHTS %></b></font></td></tr> <% } %>

    <TR><TD colspan='6' id='errorMessagesId'><chiptrack:errors styleClass='NArialL' /></TD></TR>
    <TR><TD colspan='6' id='infoMessagesId'><chiptrack:info styleClass='NArialL' /> </TD></TR>
    <TR><TD colspan='6' class="NArialL"><FONT id="errorsId"  color='FF0000'></FONT></TD></TR>
	<TR>
		<TD colspan="6" bgColor=#993333   height="16"><FONT class="NArialL" style="color:#ffffff">&nbsp;User Defined Fields Entry</FONT></TD>
	</TR>
	<TR>
		<TD>&nbsp;</TD>
	</TR>
	<TR>
		<TD class="NArialL"> List1:</TD>
		<TD >
            <nested:text property="userList1" styleClass="txfield" maxlength="25"/>
        </TD>
		<TD><a href="javascript:openFieldValuesWindow(forecastSetupUserDefinedFieldsForm.userList1)"><img src="<%=chipTracContext%>/jsp/includes/images/arrow.gif" border="0" height="12" width="18" onmouseover="window.status='Click here to add values to List box';return true" title="Click here to add values to List box" onmouseout="window.status='';return true"></a></a></TD>
		<TD>&nbsp;</TD>
		<TD class="NArialL">Text Field1:</TD>
		<TD><nested:text property="userText1" styleClass="txfield" maxlength="25"/>
        </TD>
	</TR>

	<TR>
		<TD class="NArialL"> Date Field1:</TD>
		<TD><nested:text property="userDate1" styleClass="txfield" maxlength="25"/>
        </TD>
		<TD>&nbsp;</TD>
		<TD>&nbsp;</TD>
		<TD class="NArialL">Text Field2:</TD>
		<TD><nested:text property="userText2" styleClass="txfield" maxlength="25"/>
        </TD>
	</TR>

	<TR>
		<TD>&nbsp;</TD>
	</TR>
	<TR>
		<TD colspan="6" align="center">
                    <chiptrack:restrictedLink functionality='<%=Functionality.FORECAST_SETUP%>' action='<%=User.USER_ACTION_MODIFY%>' href="javascript:onSubmit();" onmouseover="window.status='Click here to Save User Defined Fields';return true" onmouseout="window.status='';return true">
					    <IMG onmouseover="window.status='Click here to save User Defined Fields';return true" title="Click here to save User Defined Fields" onmouseout="window.status='';return true" height=17 src="<%=chipTracContext%>/jsp/includes/images/save.gif" width=53 border=0>
                    </chiptrack:restrictedLink>&nbsp;&nbsp;&nbsp;&nbsp;
					<a href="javascript:document.forecastSetupUserDefinedFieldsForm.reset();"><IMG onmouseover="window.status='Click here to Reset';return true" title="Click here to Reset" onmouseout="window.status='';return true" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" border=0></a>&nbsp;&nbsp;&nbsp;&nbsp;
					<a href="javascript:closeWindow()"><IMG onmouseover="window.status='Click here to Cancel';return true" title="Click here to Cancel " src="<%=chipTracContext%>/jsp/includes/images/cancel.gif" border=0></a></TD>
	</TR>
    <TR>
        <TD>&nbsp;</TD>
    </TR>
    <TR>
        <TD colspan="6" align="right"><a href="javascript:closeWindow();"  onMouseOver="window.status='Click here to close this window';return true" title="Click here to close this window"><font class='NArialL'><b>[Close this window]</b></font></a></TD>
    </TR>
</TABLE>
    <font id="hiddenFieldsId"></font>
</html:form>
</center>
</BODY>
</HTML>
